package work.comeback.omo.util.configuration.service;

import work.comeback.omo.util.configuration.interfaces.ConfigParser;
import work.comeback.omo.util.configuration.model.ConfigOptions;

import java.lang.reflect.Field;
import java.util.Properties;

public class ReflectiveConfigParser implements ConfigParser {

    @Override
    public void parse(Properties properties, ConfigOptions configOptions) {
        // get ConfigOptions class
        Class<?> clazz = configOptions.getClass();

        // for ConfigOptions filed
        for (Field field : clazz.getDeclaredFields()) {
            String fieldName = field.getName();
            String value = properties.getProperty(fieldName);

            if (value != null) {
                try {
                    field.setAccessible(true);
                    field.set(configOptions, value);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Error setting field value: " + fieldName, e);
                }
            }
        }
    }
}
